// pages/food/food.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    foodItem: {},
    loveFood: false,
    showComment: false,
    noComment: "",
    commentList: [],
  },

  getComment: function(e) {
    const foodData = this.data.foodItem;
    wx.request({
      url: 'http://localhost:9090/comment/queryComment',
      method: 'POST',
      data: foodData,
      header: {
        'Content-Type': 'application/json'
      },
      success:(res) => {
        // console.log(res.data)
        if(res.data.code == "200") {
          this.setData({
            showComment: true,
            commentList: res.data.data
          })
        } else if(res.data.code == "404") {
          this.setData({
            noComment: res.data.msg,
          })
          console.log(this.data.noComment)
        }
        // console.log(this.data.commentList)
      },
    })
  },

  getCollect: function(e) {
    if(!app.globalData.userID) {
      wx.navigateTo({
        url: '../login/login',
      })
    } else {
      const collectData = {
        collectFood: this.data.foodItem.foodID,
        collectUser: app.globalData.userID
      };
      wx.request({
        url: 'http://localhost:9090/collect/getCollectState',
        method: 'POST',
        data: collectData,
        header: {
          'Content-Type': 'application/json'
        },
        success:(res) => {
          if(res.data.code == "200") {
            this.setData({
              loveFood: res.data.data
            })
          }
        }
      })
    }
  },

  collectFood: function(e) {
    const collectData = {
      collectFood: this.data.foodItem.foodID,
      collectUser: app.globalData.userID
    };
    if(this.data.loveFood) {
      wx.request({
        url: 'http://localhost:9090/collect/deleteCollect',
        method: 'POST',
        data: collectData,
        header: {
          'Content-Type': 'application/json'
        },
        success:(res) => {
          if(res.data.code == "200") {
            this.setData({
              loveFood: false
            })
          }
        }
      })
    } else {
      wx.request({
        url: 'http://localhost:9090/collect/addCollect',
        method: 'POST',
        data: collectData,
        header: {
          'Content-Type': 'application/json'
        },
        success:(res) => {
          if(res.data.code == "200") {
            this.setData({
              loveFood: true
            })
          }
        }
      })
    }
  },

  getFormattedDate: function() {
    const now = new Date();
    const year = now.getFullYear();
    const month = String(now.getMonth() + 1).padStart(2, '0');
    const day = String(now.getDate()).padStart(2, '0');
    const hours = String(now.getHours()).padStart(2, '0');
    const minutes = String(now.getMinutes()).padStart(2, '0');
    return `${year}-${month}-${day} ${hours}:${minutes}`;
  },

  payMoney: function(e) {
    const orderData = {
      orderFood: this.data.foodItem.foodID,
      orderUser: app.globalData.userID,
      orderDate: this.getFormattedDate(),
      orderState: "支付成功"
    }
    wx.request({
      url: 'http://localhost:9090/order/addOrder',
      method: 'POST',
      data: orderData,
      header: {
        'Content-Type': 'application/json'
      },
      success:(res) => {
        wx.showToast({
          icon: 'success',
          title: '支付成功',
        })
      },
    })
  },

  payFood: function(e) {
    wx.showModal({
      title: '模拟支付',
      content: '确定要购买一份此菜品吗？',
      showCancel: true,  
      cancelText: '取消', 
      confirmText: '确定',
      success:(res) => {
          if (res.confirm) {
            this.payMoney()
          } else if (res.cancel) {
            console.log('用户点击了“取消”按钮');
          }
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.food) {
      const foodItem = JSON.parse(options.food);
      this.setData({
        foodItem: foodItem
      });
    }
    this.getComment();
    this.getCollect();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})